package com.doform.android.xianggucheng.activity.downorder;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.activity.BaseTitleActivity;
import com.doform.android.xianggucheng.bean.AddressBean;
import com.doform.android.xianggucheng.bean.ClientBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqListAddress;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.utils.Constans;
import com.doform.android.xianggucheng.widget.DividerItemDecoration;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017/6/3.
 * GitHub：https://github.com/cxydxpx
 */

public class ListAdressActivity extends BaseTitleActivity {

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @BindView(R.id.tv_name)
    TextView mName;

    @BindView(R.id.btn_add)
    Button btn_add;

    private List<AddressBean> mDatas = new ArrayList<>();
    private CommonAdapter<AddressBean> mCommonAdapter;
    private String mId;
    private String name;
    private ClientBean.ListBean mClientBean;

    @Override
    protected int layoutResId() {
        return R.layout.activity_select_address;
    }

    @Override
    protected void init() {
        ClientBean.ListBean bean = (ClientBean.ListBean) getIntent().getSerializableExtra(Constans.EXTRA_SELECT_ADDRESS_CLIENT_BEAN);
        if (null != bean) {
            this.mClientBean = bean;

            mId = mClientBean.getId();
            name = mClientBean.getName();

        }
    }

    @Override
    protected void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    }

    @Override
    protected void updateData() {
        super.updateData();
        getDataForService(mId);
    }

    private void getDataForService(String mId) {
        HttpManager.instance()
                .getAddress(new ReqListAddress(mId))
                .subscribe(new ErrorHandleSubscriber<ArrayList<AddressBean>>() {
                    @Override
                    public void onNext(@NonNull ArrayList<AddressBean> addressBean) {
                        parseData(addressBean);
                    }
                });
    }

    private void parseData(List<AddressBean> mess) {
        mDatas.clear();
        for (int i = 0; i < mess.size(); i++) {

            AddressBean addressBean = mess.get(i);

            if (TextUtils.isEmpty(addressBean.getAddr()) &&
                    TextUtils.isEmpty(addressBean.getLnkname()) &&
                    TextUtils.isEmpty(addressBean.getMobile())) {
            } else {
                mDatas.add(addressBean);
            }

        }


        mCommonAdapter.notifyDataSetChanged();

    }

    @Override
    protected void initListener() {
        initAdater();
    }

    private void initAdater() {

        mCommonAdapter = new CommonAdapter<AddressBean>(this, R.layout.activity_select_address_item, mDatas) {
            @Override
            protected void convert(ViewHolder holder, final AddressBean messBean, final int position) {
                TextView tv_name = holder.getView(R.id.tv_name);
                TextView tv_address = holder.getView(R.id.tv_address);
                TextView tv_phone = holder.getView(R.id.tv_phone);
                Button btn_delete = holder.getView(R.id.btn_delete);
                Button btn_edit = holder.getView(R.id.btn_edit);

                tv_name.setText(messBean.getLnkname());
                tv_address.setText(messBean.getAddr());
                tv_phone.setText(messBean.getMobile());

                btn_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showDialog(messBean);
                    }
                });

                btn_edit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        editItem(messBean);
                    }
                });
            }
        };

        mCommonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Intent intent = new Intent(ListAdressActivity.this, SubmitOrderActivity.class);
                intent.putExtra(Constans.EXTRA_SUBMIT_ORDER, mDatas.get(position));
                intent.putExtra(Constans.EXTRA_SUBMIT_ORDER_CLIENT_ID, mClientBean.getId());
                startActivity(intent);
                finish();

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });


        mRecyclerView.setAdapter(mCommonAdapter);
    }

    //编辑条目
    private void editItem(AddressBean adressBean) {

        Intent intent = new Intent(this, UpdateAddressActivity.class);
        intent.putExtra(Constans.EXTRA_UPDATE_ADDRESS, adressBean);
        intent.putExtra(Constans.EXTRA_UPDATE_ADDRESS2_ID, mClientBean);
        startActivity(intent);

    }

    private void showDialog(final AddressBean clientBean) {

        new AlertDialog.Builder(this)
                .setMessage("该数据删除后将无法恢复，请确认是否要删除?").setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteItem(clientBean);
                    }
                }).
                setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        }).show();
    }

    //删除条目
    private void deleteItem(AddressBean messBean) {
        HttpManager.instance()
                .deleteAddress(messBean.getId(), messBean.getGuid())
                .subscribe(new ErrorHandleSubscriber<Integer>() {
                    @Override
                    public void onNext(@NonNull Integer integer) {
                        getDataForService(mId);
                    }
                });
    }


    @Override
    protected void showView() {
        mName.setText(name);
        titleBar.setTextTitle("选择送货地址");
    }

    @OnClick({R.id.btn_add})
    protected void processClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add:
                Intent intent = new Intent(this, NewAddressActivity.class);
                intent.putExtra(Constans.EXTRA_ADD_ADDRESS_ID, mClientBean.getId());
                intent.putExtra(Constans.EXTRA_ADD_ADDRESS_GUID, mClientBean.getGuid());
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
